package cn.miao.photoprinter.adapter;

import java.util.ArrayList;
import java.util.List;
import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import cn.miao.photoprinter.R;
import cn.miao.photoprinter.database.DatabaseHelper;
import cn.miao.photoprinter.entity.ShoppingCart;
import cn.miao.photoprinter.view.SwipeListView;

public class ShoppingCartAdapter extends BaseAdapter {

	private LayoutInflater infalter;
	private Context context;
	private List<ShoppingCart> shoppingCart = new ArrayList<ShoppingCart>();
	private int mRightWidth = 0;
	
	public ShoppingCartAdapter(Context context, int mRightWidth) {
		infalter = LayoutInflater.from(context);
		this.mRightWidth = mRightWidth;
		this.context = context;
	}

	public ShoppingCartAdapter(Context context, List<ShoppingCart> ShoppingCarts, int mRightWidth) {
		infalter = LayoutInflater.from(context);
		this.mRightWidth = mRightWidth;
		this.context = context;
		this.shoppingCart = ShoppingCarts;
	}

	public void setOnItemRightMenuClickListener(
			OnItemRightMenuClickListener mListener) {
		this.mListener = mListener;
	}

	/**
	 * 单击事件监听器
	 */
	private OnItemRightMenuClickListener mListener = null;

	public interface OnItemRightMenuClickListener {
		void onDeleteClick(View v, int position);

		void onMoreClick(View v, int position);
	}

	@Override
	public int getCount() {
		return shoppingCart.size();
	}

	@Override
	public ShoppingCart getItem(int position) {
		return this.shoppingCart.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public List<ShoppingCart> getAllCart() {
		return shoppingCart;
	}

	public void removeShoppingCart(int position) {
		this.shoppingCart.remove(position);
		notifyDataSetChanged();
	}

	public void addAll(List<ShoppingCart> paths) {
		this.shoppingCart.clear();
		this.shoppingCart.addAll(paths);
		notifyDataSetChanged();
	}

	public List<ShoppingCart> getCheckedItems() {
		List<ShoppingCart> rtlist = new ArrayList<ShoppingCart>();
		for (ShoppingCart item : shoppingCart) {
			if (item.isChecked()) {
				rtlist.add(item);
			}
		}
		return rtlist;
	}

	@Override
	public View getView(final int position, View convertView,
			final ViewGroup parent) {
		final ViewHolder holder;
		if (null == convertView) {
			convertView = infalter.inflate(R.layout.item_shoppingcart, parent,
					false);
			holder = new ViewHolder();
			holder.itemview = (View) convertView.findViewById(R.id.itemview);
			holder.frontview = (View) convertView.findViewById(R.id.front);
			holder.backview = (View) convertView.findViewById(R.id.back);
			holder.tvShop = (TextView) convertView.findViewById(R.id.tvshop);
			holder.tvNum = (TextView) convertView.findViewById(R.id.tvNum);
			holder.btAdd = (Button) convertView.findViewById(R.id.btAddNum);
			holder.btMinus = (Button) convertView.findViewById(R.id.btMinusNum);
			holder.btDelete = (Button) convertView.findViewById(R.id.btremove);
			holder.tvPhotoinfo = (TextView) convertView
					.findViewById(R.id.tvphotoinfo);
			holder.cbSelect = (CheckBox) convertView
					.findViewById(R.id.cbselect);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
			holder.cbSelect.setOnCheckedChangeListener(null);
			holder.cbSelect.setChecked(false);
		}
		
		LinearLayout.LayoutParams lp1 = new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT);
		holder.frontview.setLayoutParams(lp1);
        LinearLayout.LayoutParams lp2 = new LayoutParams(mRightWidth, LayoutParams.MATCH_PARENT);
        holder.backview.setLayoutParams(lp2);
		final ShoppingCart record = shoppingCart.get(position);
		holder.tvShop.setText(record.getShop());
		holder.tvNum.setText("" + record.getPhotocnt());
		holder.tvPhotoinfo.setText(record.getPhotoBrand()
				+ record.getPhotoMatieral() + ", " + record.getPhotoSize());
		holder.btAdd.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				addNum(holder.tvNum);
			}
		});
		holder.btMinus.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				minusNum(holder.tvNum);
			}
		});
		holder.btDelete.setOnClickListener(new OnClickListener() {

			@SuppressLint("NewApi")
			@Override
			public void onClick(View v) {
				System.out.println("remove:" + record.getId());
				((SwipeListView) parent).hiddenRight(holder.itemview);
				new DatabaseHelper(context).deleteData(record.getId());
				removeShoppingCart(position);
				// ToastUtil.showTips(context, R.drawable.tips_smile, "delete "
				// + record.getShop());
			}
		});
		holder.cbSelect
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						getItem(position).setChecked(isChecked);
					}
				});
		if (record.isChecked()) {
			holder.cbSelect.setChecked(true);
		} else {
			holder.cbSelect.setChecked(false);
		}
		return convertView;
	}

	private void addNum(TextView tvNum) {
		int curVal = Integer.parseInt(tvNum.getText().toString());
		tvNum.setText("" + (curVal + 1));
	}

	private void minusNum(TextView tvNum) {
		int curVal = Integer.parseInt(tvNum.getText().toString());
		if (curVal > 1) {
			tvNum.setText("" + (curVal - 1));
		}
	}

	class ViewHolder {
		View itemview;
		
		View frontview;
		View backview;

		TextView tvShop;
		TextView tvPhotoinfo;
		TextView tvNum;
		Button btAdd;
		Button btMinus;
		Button btDelete;
		CheckBox cbSelect;
	}

}
